#!/usr/bin/python3
# -*- coding: utf8 -*-
# Date   : 2021/02/12
# Author : Kwan
# Email  : allenxian@live.com
# Blog   : https://my.oschina.net/u/4173440

from flask import Blueprint, render_template, redirect, url_for, request, json
from flask_login import login_required, current_user
from flasky.common import admin_required, app_logger

from .axios import AxiosManager


# 系统设置
carpark = Blueprint(name='carpark', import_name='flasky', url_prefix='/carpark', template_folder='views/templates')
CarParkAxios = AxiosManager()


@carpark.route('/')
@login_required
@admin_required
def default():
    """
    系统设置
    """
    return redirect(url_for('carpark.setting'))


@carpark.route('/setting', methods=['POST', 'GET'])
@login_required
@admin_required
def setting():
    """
    系统设置
    """
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'setting'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：carpark.setting 数据：{p_data}")
            return CarParkAxios.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：carpark.setting 数据：{p_data} 错误：{e}")
        return CarParkAxios.error('處理錯誤，請聯係管理員')
    # GET
    return render_template('carpark/setting.html', **CarParkAxios.app_info_page_data())


@carpark.route('/network', methods=['POST', 'GET'])
@login_required
@admin_required
def network():
    """
    网络设置
    """
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'network'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：carpark.network 数据：{p_data}")
            return CarParkAxios.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：carpark.network 数据：{p_data} 错误：{e}")
        return CarParkAxios.error('處理錯誤，請聯係管理員')
    return render_template('carpark/network.html', **CarParkAxios.network_page_data())


@carpark.route('/users', methods=['POST', 'GET'])
@login_required
@admin_required
def users():
    """
    系统用户
    """
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'users'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：carpark.users 数据：{p_data}")
            return CarParkAxios.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：carpark.users 数据：{p_data} 错误：{e}")
        return CarParkAxios.error('處理錯誤，請聯係管理員')
    return render_template('carpark/users.html', **CarParkAxios.users_page_data())


@carpark.route('/logs', methods=['POST', 'GET'])
@login_required
@admin_required
def logs():
    """
    运行日志
    """
    page = int(request.args.get('page', 1))
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = 'logs'
            app_logger.debug(f"用户：{current_user.LoginName } 接口：carpark.users 数据：{p_data}")
            return CarParkAxios.messages(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：carpark.users 数据：{p_data} 错误：{e}")
        return CarParkAxios.error('處理錯誤，請聯係管理員')
    return render_template('carpark/logs.html', **CarParkAxios.log_views_page_data(page))
